/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.analysismodel.AxiSymetricAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneStrainAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneStressAnalysisM;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public class NodeForcesDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private String gAnlType;
	
	private DecimalField FxField;
	private DecimalField FyField;
	private DecimalField FzField;
	private DecimalField MxField;
	private DecimalField MyField;
	private DecimalField MzField;
	public double Fx;
	public double Fy;
	public double Fz;
	public double Mx;
	public double My;
	public double Mz;
	
//*****************************************************************************
	
	public NodeForcesDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		gAnlType = parent.getModel().getDiscreteModel().getGlobalAnalysisModel().getType();
		
		setUpFormat();
		setUpFields();
		
		JPanel forcePane = new JPanel();
		forcePane.setPreferredSize(new Dimension(360, 280));
		forcePane.setLayout(new GridLayout(4,1,2,2));
		JPanel forceFPane = new JPanel();
		forceFPane.setLayout(new GridLayout(1,3,2,2));
		forcePane.add(forceFPane);
		JPanel forceValueFPane = new JPanel();
		forceValueFPane.setLayout(new GridLayout(1,3,2,2));
		forcePane.add(forceValueFPane);
		JPanel forceMPane = new JPanel();
		forceMPane.setLayout(new GridLayout(1,3,2,2));
		forcePane.add(forceMPane);
		JPanel forceValueMPane = new JPanel();
		forceValueMPane.setLayout(new GridLayout(1,3,2,2));
		forcePane.add(forceValueMPane);
		
		forceFPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/ForceX64.gif")) ));
		forceFPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/ForceY64.gif")) ));
		forceFPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/ForceZ64.gif")) ));
		forceMPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/MomentX64.gif")) ));
		forceMPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/MomentY64.gif")) ));
		forceMPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/MomentZ64.gif")) ));
		
		JPanel forceFxPane = new JPanel();
		JPanel forceFyPane = new JPanel();
		JPanel forceFzPane = new JPanel();
		JPanel forceMxPane = new JPanel();
		JPanel forceMyPane = new JPanel();
		JPanel forceMzPane = new JPanel();
		
		forceFxPane.add(new JLabel("Fx:"));
		forceFyPane.add(new JLabel("Fy:"));
		forceFzPane.add(new JLabel("Fz:"));
		forceMxPane.add(new JLabel("Mx:"));
		forceMyPane.add(new JLabel("My:"));
		forceMzPane.add(new JLabel("Mz:"));
		
		forceFxPane.add(FxField);
		forceFyPane.add(FyField);
		forceFzPane.add(FzField);
		forceMxPane.add(MxField);
		forceMyPane.add(MyField);
		forceMzPane.add(MzField);
		
		forceValueFPane.add(forceFxPane);
		forceValueFPane.add(forceFyPane);
		forceValueFPane.add(forceFzPane);
		forceValueMPane.add(forceMxPane);
		forceValueMPane.add(forceMyPane);
		forceValueMPane.add(forceMzPane);
		
		if (gAnlType.equals("PlaneFrame")) {
			FzField.setEnabled(false);
			MxField.setEnabled(false);
			MyField.setEnabled(false);
		}
		else if (gAnlType.equals("PlaneTruss")) {
			FzField.setEnabled(false);
			MxField.setEnabled(false);
			MyField.setEnabled(false);
			MzField.setEnabled(false);
		}
		else if (gAnlType.equals("Beam")) {
			FxField.setEnabled(false);
			FzField.setEnabled(false);
			MxField.setEnabled(false);
			MyField.setEnabled(false);
		}
		else if (gAnlType.equals("Grid")) {
			FxField.setEnabled(false);
			FzField.setEnabled(false);
			MyField.setEnabled(false);
		}
		else if((parent.getModel().getDiscreteModel().getGlobalAnalysisModel() instanceof PlaneStrainAnalysisM)|| 
				(parent.getModel().getDiscreteModel().getGlobalAnalysisModel() instanceof PlaneStressAnalysisM)||
				(parent.getModel().getDiscreteModel().getGlobalAnalysisModel() instanceof AxiSymetricAnalysisM)){
			FzField.setEnabled(false);
			MxField.setEnabled(false);
			MyField.setEnabled(false);
			MzField.setEnabled(false);
		}
		setPane(forcePane);
	}
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(this.parent, parent.getResourceBundle().getString("dNForcesDialog01"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		try {
			Fx = FxField.getValue();
			Fy = FyField.getValue();
			Fz = FzField.getValue();
			Mx = MxField.getValue();
			My = MyField.getValue();
			Mz = MzField.getValue();
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		if (this.parent.getController().getSelection().size() == 1 ) {
			Node node = (Node)this.parent.getController().getSelection().get(0);
			
			FxField = new DecimalField(node.getForce(0), 7, numberFormat);
			FyField = new DecimalField(node.getForce(1), 7, numberFormat);
			FzField = new DecimalField(node.getForce(2), 7, numberFormat);
			MxField = new DecimalField(node.getForce(3), 7, numberFormat);
			MyField = new DecimalField(node.getForce(4), 7, numberFormat);
			MzField = new DecimalField(node.getForce(5), 7, numberFormat);
			
		}else {
			
			FxField = new DecimalField(0, 7, numberFormat);
			FyField = new DecimalField(0, 7, numberFormat);
			FzField = new DecimalField(0, 7, numberFormat);
			MxField = new DecimalField(0, 7, numberFormat);
			MyField = new DecimalField(0, 7, numberFormat);
			MzField = new DecimalField(0, 7, numberFormat);
			
		}
	}
	
//*****************************************************************************
}
